package com.bestcem.xm.openplatform.controller;

import cn.hutool.core.lang.ObjectId;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bestcem.xm.common.core.domain.web.XmResultJson;
import com.bestcem.xm.component.security.context.SecurityContextHolder;
import com.bestcem.xm.openplatform.entity.*;
import com.bestcem.xm.openplatform.service.OpenplatformExtparamService;
import com.bestcem.xm.openplatform.service.OpenplatformExtparamenummanageService;
import com.bestcem.xm.openplatform.util.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Slf4j
@Api(tags = "外部参数")
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/openplatform/")
public class OpenplatformExtparamController {

    private @NonNull OpenplatformExtparamService openplatformExtparamService;
    private @NonNull OpenplatformExtparamenummanageService openplatformExtparamenummanageService;

    @ApiOperation(value = "外部参数-获取列表")
    @GetMapping(value = {"/custom/v2/extparam/{id:[a-fA-F0-9]{24}}/", "/custom/v2/extparam/"})
    public XmResultJson<Object> getOpenplatformExtparam(@PathVariable(value = "id", required = false) String extparamId,
                                                        @ApiIgnore Page<OpenplatformExtparam> page,
                                                        HttpServletRequest request) {
        //当url指定id时，直接查询该id的ExtParam，忽略其他筛选参数
        // 当url没有指定id时，返回分页查询结果，参数在query中
        if (ObjectId.isValid(extparamId)) {
            return XmResultJson.success(openplatformExtparamService.getOpenplatformExtparamById(extparamId));
        }
        if (!PageUtil.isPageQuery(request)) {
            return XmResultJson.success(openplatformExtparamService.getOpenplatformExtparamList(request));
        } else {
            return XmResultJson.success(openplatformExtparamService.getOpenplatformExtparamPage(page, request));
        }
    }

    @ApiOperation(value = "外部参数-新增外部参数")
    @PostMapping(value = {"/custom/v2/extparam/"})
    public XmResultJson<Map<String, String>> postOpenplatformExtparam(@RequestBody JSONObject param) {
        return XmResultJson.success(openplatformExtparamService.postOpenplatformExtparam(param));
    }

    @ApiOperation(value = "外部参数-编辑外部参数")
    @PutMapping(value = {"/custom/v2/extparam/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<Map<String, String>> putOpenplatformExtparam(@PathVariable(value = "id") String extparamId,
                                                                     @RequestBody JSONObject param) {
        return XmResultJson.success(openplatformExtparamService.putOpenplatformExtparam(extparamId, param));
    }

    @ApiOperation(value = "外部参数-删除外部参数")
    @DeleteMapping(value = {"/custom/v2/extparam/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<Map<String, Integer>> deleteOpenplatformExtparam(@PathVariable(value = "id") String extparamId) {
        return XmResultJson.success(openplatformExtparamService.deleteOpenplatformExtparam(extparamId));
    }

    @ApiOperation(value = "外部参数-校验外部参数")
    @PostMapping(value = {"/custom/v2/extparam/check/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<Map<String, Object>> checkOpenplatformExtparam(@PathVariable(value = "id") String extparamId) {
        return XmResultJson.success(openplatformExtparamService.checkOpenplatformExtparam(extparamId));
    }

    @ApiOperation(value = "外部参数-获取加密配置")
    @GetMapping(value = {"/custom/v2/encryptionconfigs/{id:[a-fA-F0-9]{24}}/", "/custom/v2/encryptionconfigs/"})
    public XmResultJson<Object> getEncryptionConfigs(@PathVariable(value = "id", required = false) String id,
                                                     @ApiIgnore Page<OpenplatformEncryptionconfig> page,
                                                     HttpServletRequest request) {
        //初始化外部参数加密配置表
        openplatformExtparamService.initOrgAesKey();
        //当url指定id时，直接查询该id的ExtParam，忽略其他筛选参数
        // 当url没有指定id时，返回分页查询结果，参数在query中
        if (ObjectId.isValid(id)) {
            return XmResultJson.success(openplatformExtparamService.getEncryptionConfigsById(id));
        }
        if (!PageUtil.isPageQuery(request)) {
            return XmResultJson.success(openplatformExtparamService.getEncryptionConfigsList(request));
        } else {
            return XmResultJson.success(openplatformExtparamService.getEncryptionConfigsPage(page, request));
        }
    }

    @ApiOperation(value = "外部参数-新增system_no枚举")
    @PostMapping(value = {"/custom/v2/systemmanages/"})
    public XmResultJson<Map<String, String>> postSystemManages(@RequestBody JSONObject param) {
        return XmResultJson.success(openplatformExtparamService.postSystemManages(param));
    }

    @ApiOperation(value = "外部参数-编辑system_no枚举")
    @PutMapping(value = {"/custom/v2/systemmanages/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<Map<String, String>> putSystemManages(@PathVariable(value = "id") String id,
                                                              @RequestBody JSONObject param) {
        return XmResultJson.success(openplatformExtparamService.putSystemManages(id, param));
    }

    @ApiOperation(value = "外部参数-获取system_no枚举")
    @GetMapping(value = {"/custom/v2/systemmanages/{id:[a-fA-F0-9]{24}}/", "/custom/v2/systemmanages/"})
    public XmResultJson<Object> getSystemManages(@PathVariable(value = "id", required = false) String id,
                                                 @ApiIgnore Page<OpenplatformSystemmanage> page,
                                                 HttpServletRequest request) {
        //当url指定id时，直接查询该id的ExtParam，忽略其他筛选参数
        // 当url没有指定id时，返回分页查询结果，参数在query中
        if (ObjectId.isValid(id)) {
            return XmResultJson.success(openplatformExtparamService.getSystemManagesById(id));
        }
        String orgId = SecurityContextHolder.getOrgId();
        if (!PageUtil.isPageQuery(request)) {
            return XmResultJson.success(openplatformExtparamService.getSystemManagesList(orgId));
        } else {
            return XmResultJson.success(openplatformExtparamService.getSystemManagesPage(page, orgId));
        }
    }

    @ApiOperation(value = "外部参数-删除system_no枚举")
    @DeleteMapping(value = {"/custom/v2/systemmanages/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<Map<String, Integer>> deleteSystemmanages(@PathVariable(value = "id") String id) {
        return XmResultJson.success(openplatformExtparamService.deleteSystemmanages(id));
    }

    @ApiOperation(value = "外部参数-新增condition_no枚举")
    @PostMapping(value = {"/custom/v2/conditionmanages/"})
    public XmResultJson<Map<String, String>> postConditionmanages(@RequestBody JSONObject param) {
        return XmResultJson.success(openplatformExtparamService.postConditionmanages(param));
    }

    @ApiOperation(value = "外部参数-获取condition_no枚举")
    @GetMapping(value = {"/custom/v2/conditionmanages/{id:[a-fA-F0-9]{24}}/", "/custom/v2/conditionmanages/"})
    public XmResultJson<Object> getConditionmanages(@PathVariable(value = "id", required = false) String id,
                                                    @ApiIgnore Page<OpenplatformConditionmanage> page,
                                                    HttpServletRequest request) {
        //当url指定id时，直接查询该id的ExtParam，忽略其他筛选参数
        // 当url没有指定id时，返回分页查询结果，参数在query中
        if (ObjectId.isValid(id)) {
            return XmResultJson.success(openplatformExtparamService.getConditionmanagesById(id));
        }
        String orgId = SecurityContextHolder.getOrgId();
        if (!PageUtil.isPageQuery(request)) {
            return XmResultJson.success(openplatformExtparamService.getConditionmanagesList(orgId));
        } else {
            return XmResultJson.success(openplatformExtparamService.getConditionmanagesPage(page, orgId));
        }
    }

    @ApiOperation(value = "外部参数-编辑condition_no枚举")
    @PutMapping(value = {"/custom/v2/conditionmanages/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<Map<String, String>> putConditionmanages(@PathVariable(value = "id") String id,
                                                                 @RequestBody JSONObject param) {
        return XmResultJson.success(openplatformExtparamService.putConditionmanages(id, param));
    }

    @ApiOperation(value = "外部参数-删除condition_no枚举")
    @DeleteMapping(value = {"/custom/v2/conditionmanages/{id:[a-fA-F0-9]{24}}"})
    public XmResultJson<Map<String, Integer>> deleteConditionmanages(@PathVariable(value = "id") String id) {
        return XmResultJson.success(openplatformExtparamService.deleteConditionmanages(id));
    }

    @ApiOperation(value = "外部参数-外部参数信息")
    @PostMapping(value = {"/custom/v2/extparam/post/"})
    public XmResultJson<Object> postExtparam(@RequestBody JSONObject param) {
        return XmResultJson.success(openplatformExtparamService.postExtparam(param));
    }

    @ApiOperation(value = "外部参数-外部参数枚举信息")
    @PostMapping(value = {"/custom/v2/extparamenum/post/"})
    public XmResultJson<Object> postExtparamenum(@RequestBody JSONObject param) {
        return XmResultJson.success(openplatformExtparamService.postExtparamenum(param));
    }

    @ApiOperation(value = "外部参数-校验")
    @GetMapping(value = {"/custom/v2/systemcondition/check/"})
    public XmResultJson<Object> getSystemconditionCheck(HttpServletRequest request) {
        return XmResultJson.success(openplatformExtparamService.getSystemconditionCheck(request));
    }

    @ApiOperation(value = "外部参数-获取功能信息")
    @GetMapping(value = {"/custom/v2/functionoptions/{id:[a-fA-F0-9]{24}}/", "/custom/v2/functionoptions/"})
    public XmResultJson<Object> getFunctionoptions(@PathVariable(value = "id", required = false) String id,
                                                   @ApiIgnore Page<OpenplatformFunctionoption> page,
                                                   HttpServletRequest request) {
        //当url指定id时，直接查询该id的ExtParam，忽略其他筛选参数
        // 当url没有指定id时，返回分页查询结果，参数在query中
        if (ObjectId.isValid(id)) {
            return XmResultJson.success(openplatformExtparamService.getFunctionoptionsById(id));
        }
        if (!PageUtil.isPageQuery(request)) {
            return XmResultJson.success(openplatformExtparamService.getFunctionoptionsList(request));
        } else {
            return XmResultJson.success(openplatformExtparamService.getFunctionoptionsPage(page, request));
        }
    }

    @GetMapping(value = {"/custom/v2/extparamenum/{id:[a-fA-F0-9]{24}}/", "/custom/v2/extparamenum/"})
    public XmResultJson<Object> getExtParamEnumVarious(@PathVariable(value = "id", required = false) String id,
                                                       @ApiIgnore Page<OpenplatformExtparamenummanage> page,
                                                       HttpServletRequest request) {
        if (ObjectId.isValid(id)) {
            return XmResultJson.success(openplatformExtparamenummanageService.getExtParamEnumById(id));
        }
        if (!PageUtil.isPageQuery(request)) {
            return XmResultJson.success(openplatformExtparamenummanageService.getExtParamEnumList(request));
        } else {
            return XmResultJson.success(openplatformExtparamenummanageService.getExtParamEnumPage(page, request));
        }
    }

    @PostMapping(value = "/custom/v2/extparamenum/")
    public XmResultJson<?> addExtParamEnum(@RequestBody JSONObject param) {
        return openplatformExtparamenummanageService.addExtParamEnum(param);
    }

    @PutMapping(value = "/custom/v2/extparamenum/{id:[a-fA-F0-9]{24}}")
    public XmResultJson<?> updateExtParamEnum(@RequestBody JSONObject param, @PathVariable(value = "id") String id) {
        return openplatformExtparamenummanageService.updateExtParamEnum(id, param);
    }

    @DeleteMapping(value = "/custom/v2/extparamenum/{id:[a-fA-F0-9]{24}}")
    public XmResultJson<?> deleteExtParamEnum(@PathVariable(value = "id") String id) {
        return openplatformExtparamenummanageService.deleteExtParamEnum(id);
    }
}